package com.alex.designPattern.prototype;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过实现克隆（cloneable）接口，实现对象的快速复制
 * @author alex
 *
 */
public class CloneableTest implements Cloneable {
	
	private String name;
	
	private Integer age;
	
	private List<String> roleName;
	
	private Family family;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public List<String> getRoleName() {
		return roleName;
	}

	public void setRoleName(List<String> roleName) {
		this.roleName = roleName;
	}

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
	
	
	
	@Override
	public String toString() {
		return "CloneableTest [name=" + name + ", age=" + age + "]";
	}

	public static void main(String[] args){
		lightClone1();
		System.out.println("\n\n");
		lightClone2();
		System.out.println("\n\n");
		lightClone3();
	}

	private static void lightClone1() {
		CloneableTest obj1 = new CloneableTest();
		obj1.setAge(10);
		obj1.setName("alex");
		System.out.println(obj1);
		
		try {
			CloneableTest obj2 = (CloneableTest)obj1.clone();
			obj2.setAge(11);
			System.out.println(obj2);
			System.out.println(obj1);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
	
	
	private static void lightClone2() {
		CloneableTest obj1 = new CloneableTest();
		List<String> rolesA = new ArrayList<String>();
		rolesA.add("roleA1");
		rolesA.add("roleA2");
		rolesA.add("roleA3");
		obj1.setRoleName(rolesA);
		
		System.out.println(obj1.getRoleName().get(0));
		
		try {
			CloneableTest obj2 = (CloneableTest)obj1.clone();
			obj2.getRoleName().set(0, "roleA1.1");
			System.out.println(obj2.getRoleName().get(0));
			System.out.println(obj1.getRoleName().get(0));
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
	
	private static void lightClone3(){
		CloneableTest obj1 = new CloneableTest();
		Family f1 = new Family();
		f1.setAddress("beijing");
		f1.setNumber(2);
		obj1.setFamily(f1);
		
		System.out.println(obj1.getFamily().getAddress());
		
		try {
			CloneableTest obj2 = (CloneableTest)obj1.clone();
			obj2.getFamily().setAddress("HeNan");
			System.out.println(obj2.getFamily().getAddress());
			System.out.println(obj1.getFamily().getAddress());
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}


class Family {
	
	private String address;
	
	private Integer number;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}
	
}
